<template>
	<div class="content">
		<Collapse v-model="value1">
			<!--<Panel name="2" style="display: none;">
				消费配置
				<p slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="">
								非第1笔
								<Input class="w80" v-model.trim="config.one.content" /> %
							</FormItem>
							<div class="p-10">
								<Button type="primary" size="large" v-on:click="btn1(config.one.id)">保　存</Button>
							</div>
						</div>
					</Form>
				</p>
				<p slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="">
								日收益封顶
								<Input class="w80" v-model.trim="config.cap.content" /> %
							</FormItem>
							<div class="p-10">
								<Button type="primary" size="large" v-on:click="btn2(config.cap.id)">保　存</Button>
							</div>
						</div>
					</Form>
				</p>
			</Panel>-->
			<Panel name="1">
				分享配置
				<div slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="等级配置：">
								收益百分比
								<Input class="w80" placeholder="请添加" v-model.trim="nodeConfig"/> %
							</FormItem>
							<div class="p-10">
								<Button type="info" size="large" v-on:click="btn6">添 加</Button>
							</div>
						</div>
						<FormItem label="收益：">
							<div class="canshu" v-for="(item,index) in config1.nodeRewardConfigs" :key="item.id">
								<FormItem>
									第
									<Input class="w50" v-model.trim="item.gradeId" disabled /> 笔消费收益
									<Input class="w80" v-model.trim="item.content"/> %
								</FormItem>
								<div class="p-10">
									<Button type="error" :disabled='index == config1.nodeRewardConfigs.length-1 ? false : true' size="large" v-on:click="btn8(item.id)">删 除</Button>
								</div>
								<div class="p-10">
									<Button type="primary" size="large" v-on:click="btn7(item.id,item.content)">保　存</Button>
								</div>
							</div>
						</FormItem>
					</Form>
				</div>
				<p slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="重复消费收益：">
								<Input class="w80" v-model.trim="config.one.content" /> %
							</FormItem>
							<div class="p-10">
								<Button type="primary" size="large" v-on:click="btn1(config.one.id)">保　存</Button>
							</div>
						</div>
					</Form>
					<span style="color: red;padding: 10px;">注：第<span v-text="config1.nodeRewardConfigs.length+1"></span>笔订单开始，其推荐用户的第一笔订单返利与第<span v-text="config1.nodeRewardConfigs.length"></span>笔一致，其他订单为重复消费</span>
				</p>
			</Panel>
			<Panel name="3">
				间接分享收益
				<div slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="等级配置：">
								收益百分比
								<Input class="w80" placeholder="请添加" v-model.trim="netConfig"/> %
							</FormItem>
							<div class="p-10">
								<Button type="info" size="large" v-on:click="btn3">添 加</Button>
							</div>
						</div>
						<FormItem label="收益：">
							<div class="canshu" v-for="(item,index) in config1.netRewardConfigs" :key="item.id">
								<FormItem>
									收益等级
									<Input class="w50" v-model.trim="item.gradeId" disabled /> 收益百分比
									<Input class="w80" v-model.trim="item.content"/> %
								</FormItem>
								<div class="p-10">
									<Button type="error" :disabled='index == config1.netRewardConfigs.length-1 ? false : true' size="large" v-on:click="btn5(item.id)">删 除</Button>
								</div>
								<div class="p-10">
									<Button type="primary" size="large" v-on:click="btn4(item.id,item.content)">保　存</Button>
								</div>
							</div>
						</FormItem>
					</Form>
				</div>
			</Panel>
		</Collapse>
	</div>

</template>
<script>
export default {
  data () {
    return {
      value1: '1', // 默认展开第一个列表
      config: { // 消费配置
        one: '', // 非第一笔
        cap: '' // 日收益封顶
      },
      config1: {
      	nodeRewardConfigs: [], // 这里必须遵循这样的写法(否则报错,但不影响使用)
      	netRewardConfigs: []
      }, // 查询推荐，分享的配置
      netConfig: '', // 添加 USDT 收益百分比(推荐配置)
      nodeConfig: '' // 添加 USDT 收益百分比(分享配置)
    }
  },
  created () {

  },
  mounted () {
    this.$Message.config({
      top: 250
    });
    this.getConfig() // 查询消费配置
    this.getConfig1() // 查询推荐，分享的配置
  },
  methods: {
    getConfig () { // 查询消费配置
      let token = localStorage.getItem('mytoken')
      this.axios.get('parameter/pay/config', {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.config = res.data.data;
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    getConfig1 () { // 查询推荐，分享的配置信息
      let token = localStorage.getItem('mytoken')
      this.axios.get('parameter/reward/config', {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.config1 = res.data.data;
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    btn1 (index) { // 第一笔
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        id: index,
        content: this.config.one.content
      })
      this.axios.put('parameter/pay/config', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.getConfig()
          this.$Message.info(res.data.message)
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    btn2 (index) { // 日封顶收益
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        id: index,
        content: this.config.cap.content
      })
      this.axios.put('parameter/pay/config', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.getConfig()
          this.$Message.info(res.data.message)
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    btn3 () { // 添加推荐收益
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        content: this.netConfig
      })
      this.axios.post('parameter/net/config', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.getConfig1()
          this.$Message.info(res.data.message)
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    btn4 (index, content) { // 修改推荐收益
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        id: index,
        content: content
      })
      this.axios.put('parameter/net/config', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.getConfig1()
          this.$Message.info(res.data.message)
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    btn5 (index) { // 删除某一条收益配置的信息
      let token = localStorage.getItem('mytoken')
      this.axios.delete('parameter/net/config/' + index, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.getConfig1()
          this.$Message.info(res.data.message)
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    btn6 () { // 添加一条分享
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        content: this.nodeConfig
      })
      this.axios.post('parameter/node/config', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.getConfig1()
          this.$Message.info(res.data.message)
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    btn7 (index, content) { // 修改一条分享
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        id: index,
        content: content
      })
      this.axios.put('parameter/node/config', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.getConfig1()
          this.$Message.info(res.data.message)
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    },
    btn8 (index) { // 删除一条分享
      let token = localStorage.getItem('mytoken')
      this.axios.delete('parameter/node/config/' + index, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        if (res.data.code == 8008) {
          this.getConfig1()
          this.$Message.info(res.data.message)
        } else if (res.data.code == 4004) {
          this.$Message.info(res.data.message)
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({
            name: 'login'
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
	.p-10 {
		padding-top: 0 !important;
		padding-left: 50px !important;
	}

	.w50 {
		width: 50px !important;
	}

	.mar {
		margin-left: 5px;
	}
</style>
